﻿/****************************************************************
 * Copyright (c) 2010-2013 Besture All Rights Reserved.
 * 
 * File Name:		ContentBll 
 * 
 * Created:		2013-7-22 16:49:46
 * Original Author:	nitao 
 * 
 * ------------------------------------------------------------
 * MODIFICATION HISTORY
 * ------------------------------------------------------------
 * When			Who		Why
 * ------------------------------------------------------------
 * 
 *
 ****************************************************************/

using System;
using System.Data;
using Besture.Common;
using Besture.Dal;
using Besture.Model;

namespace Besture.Bll
{
    public class ContentBll : BaseFacade
    {
        private readonly ContentDal _contentDal = new ContentDal();

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(ContentModel model)
        {
            try
            {
                base.CreateConnection();
                base.BeginTransaction();
                int lsRecord = 0;
                lsRecord = _contentDal.Add(model);
                if (lsRecord > 0)
                {
                    base.Commit();
                    return lsRecord;
                }
                else
                {
                    base.RollBack();
                    return -1;
                }
            }
            catch (Exception e)
            {
                base.RollBack();
                throw e;
            }
            finally
            {
                base.CloseConnection();
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(ContentModel model)
        {
            try
            {
                base.CreateConnection();
                base.BeginTransaction();
                int lsRecord = 0;
                lsRecord = _contentDal.Update(model);
                if (lsRecord > 0)
                {
                    base.Commit();
                    return lsRecord;
                }
                else
                {
                    base.RollBack();
                    return -1;
                }
            }
            catch (Exception e)
            {
                base.RollBack();
                throw e;
            }
            finally
            {
                base.CloseConnection();
            }
        }



        /// <summary>
        /// 根据id获取单条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ContentModel GetModel(int id)
        {
            try
            {
                CreateConnection();
                return _contentDal.GetModel(id);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                CloseConnection();
            }
        }

        /// <summary>
        /// 获取全部数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetDataTable()
        {
            try
            {
                CreateConnection();
                return _contentDal.GetDataTable();
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                CloseConnection();
            }
        }

        /// <summary>
        /// 单个删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(int id)
        {
            try
            {
                CreateConnection();
                return _contentDal.Delete(id);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                CloseConnection();
            }
        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public int DeleteList(string ids)
        {
            try
            {
                CreateConnection();
                return _contentDal.DeleteList(ids);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                CloseConnection();
            }
        }



        public void SetContent(string entry, string content, string entryTitle)
        {
            try
            {
                // create the connection to the database
                CreateConnection();
                _contentDal.SetContent(entry, content, entryTitle);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                // close the connection with the database
                CloseConnection();
            }
        }

        public ContentModel GetContent(string entry)
        {
            try
            {
                // create the connection to the database
                CreateConnection();
                return _contentDal.GetContent(entry);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                // close the connection with the database
                CloseConnection();
            }
            return new ContentModel();
        }
    }
}
